package com.wltt.product.ahuthr.excel;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.wltt.product.ahuthr.enums.SalaryStatusEnum;
import org.apache.poi.ss.usermodel.Cell;

import java.util.List;

public class SalaryExportHandler implements CellWriteHandler {

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                 List<WriteCellData<?>> cellDataList,
                                 Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {

        if (isHead) {
            return;
        }

        if ("salaryStatus".equals(head.getFieldName())) {
            String desc = getSalaryStatus(cell);
            cell.setCellValue(desc);
            return;
        }

        if ("isAgreeSalary".equals(head.getFieldName())) {
            String desc = getYesNo(cell);
            cell.setCellValue(desc);
            return;
        }

        if ("isTeachSeries".equals(head.getFieldName())) {
            String desc = getYesNo(cell);
            cell.setCellValue(desc);
            return;
        }

        if ("governmentStaff".equals(head.getFieldName())) {
            String desc = getYesNo(cell);
            cell.setCellValue(desc);
            return;
        }

        if ("stopType".equals(head.getFieldName())) {
            String desc = getStopType(cell);
            cell.setCellValue(desc);
            return;
        }

        if ("retireType".equals(head.getFieldName())) {
            String desc = getRetireType(cell);
            cell.setCellValue(desc);
            return;
        }
    }


    private String getSalaryStatus(Cell cell) {
        try {
            int value = (int) cell.getNumericCellValue();
            return switch (value) {
                case 0 -> "未起薪";
                case 200 -> "正常";
                case 400 -> "退休";
                case 500 -> "离休";
                case 600 -> "停薪";
                default -> "其他";
            };
        } catch (Exception e) {
            return cell.getStringCellValue();
        }
    }


    private String getYesNo(Cell cell) {
        try {
            int value = (int) cell.getNumericCellValue();
            return switch (value) {
                case 0 -> "否";
                case 1 -> "是";
                default -> "未知";
            };
        } catch (Exception e) {
            return cell.getStringCellValue();
        }
    }


    private String getStopType(Cell cell) {
        try {
            int value = (int) cell.getNumericCellValue();
            return switch (value) {
                case 0 -> "";
                case 1 -> "离职";
                case 2 -> "退休去世";
                case 3 -> "在职去世";
                default -> "其他";
            };
        } catch (Exception e) {
            return cell.getStringCellValue();
        }
    }


    private String getRetireType(Cell cell) {
        try {
            int value = (int) cell.getNumericCellValue();
            return switch (value) {
                case 0 -> "";
                case 1 -> "到年龄退休";
                case 2 -> "提前退休";
                default -> "其他";
            };
        } catch (Exception e) {
            return cell.getStringCellValue();
        }
    }

}
